<?php
namespace app\index\controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Controller;

class Product extends Base
{
    // 新增修改产品分类
    public function productTypeAdd()
    {
        $data = input();//token凭证 id pid name desc status

        if (empty($data['name'])) {
            apiReturn(201,'分类名称不能为空');
        }
        $data['date'] = time();
        if ($data['pid'] == '顶级分类') {
            $data['pid'] = 0;
        }else{
            $data['pid'] = db('crm_product_type')->where('name',$data['pid'])->value('id');
        }
        if (empty($data['id'])) {
            db('crm_product_type')->insert($data);
            apiReturn(200,'新增产品分类成功');
        }else{
            db('crm_product_type')->where('id',$data['id'])->update($data);
            apiReturn(200,'修改产品分类成功');
        }
    }

    // 父级产品分类列表
    public function productTypePidList()
    {
        $data = input();//token
        $productTypePidList = db('crm_product_type')->order('id ASC')->select();
        $array[] = array('id'=>0,'name'=>'顶级分类','pid'=>-1);
        foreach ($productTypePidList as $key => $value) {
            $array[] = $value;
        }
        apiReturn(200,'获取父级产品分类列表成功',$array);
    }

    // 产品分类列表接口
    public function productTypeList()
    {
        $data = input();//token
        $productTypeList = db('crm_product_type')->order('id ASC')->select();
        apiReturn(200,'获取产品分类列表接口成功',$productTypeList);
    }

    // 获取产品分类信息
    public function productTypeInfo()
    {
        $data = input();//token id
        $productTypeInfo = db('crm_product_type')->where('id',$data['id'])->find();
        if ($productTypeInfo['pid'] == 0) {
            $productTypeInfo['pid_name'] = '顶级分类';
        }else{
            $productTypeInfo['pid_name'] = db('crm_product_type')->where('id',$productTypeInfo['pid'])->value('name');
        }
        apiReturn(200,'获取产品分类信息成功',$productTypeInfo);
    }

    // 修改产品分类状态
    public function productTypeStatus()
    {
        $data = input();//token id
        $status = db('crm_product_type')->where('id',$data['id'])->value('status');
        if ($status == 1) {
            $datas['status'] = 0;
        }else{
            $datas['status'] = 1;
        }
        db('crm_product_type')->where('id',$data['id'])->update($datas);
        apiReturn(200,'修改成功');
    }

    // 删除产品分类接口
    public function productTypeDelete()
    {
        $data = input();//token id
        if (empty($data['id'])) {
            apiReturn(201,'产品分类id为空');
        }
        db('crm_product_type')->where('id',$data['id'])->delete();
        apiReturn(200,'删除产品分类成功');
    }

    // 新增修改产品
    public function productAdd()
    {
        $file = request()->file();
        if($file){
            try{
                if(count($file)>0){
                    foreach ($file as $value){
                        $data['thumb'] =  uploadOss($value);
                    }
                }else{
                    apiReturn(201,'请上传文件');
                }
            }catch (\Exception $e){
                apiReturn(201,'文件格式上传错误');
            }
        }
        $data = input();//token凭证 id type_id name sn pic price desc status
        if (empty($data['name'])) {
            apiReturn(201,'产品名称不能为空');
        }
        if (empty($data['sn'])) {
            apiReturn(202,'产品货号不能为空');
        }
        if (empty($data['type_id'])) {
            apiReturn(203,'产品分类不能为空');
        }
        if ($data['price'] === '') {
            apiReturn(204,'产品价格不能为空');
        }
//        if (!empty($data['pic'])) {
//           $this->thumbPic($data['pic'], 200, 112);
//        }
        if (empty($data['id'])) {
            $product = db('crm_product')->where(array('sn'=>$data['sn']))->find();
            if (!empty($product)) {
                apiReturn(201,'货号已存在');
            }
            $data['date'] = time();
            db('crm_product')->insert($data);
            apiReturn(200,'新增产品成功');
        }else{
            $product = db('crm_product')->where(array('sn'=>$data['sn']))->find();
            if ($product['sn'] != $data['sn']) {
                if (!empty($product)) {
                    apiReturn(201,'货号已存在');
                }
            }
            if (isset($data['date'])) {
                unset($data['date']);
            }
            db('crm_product')->where('id',$data['id'])->update($data);
            apiReturn(200,'修改产品成功');
        }
    }

    // 产品列表接口
    public function productList()
    {
        $data = input();//token name type_id ids rows page

        $where = array();
        // $manage_author = $this->getManageAuthor();
        // if ($manage_author['manage_type'] == 2) {
        //     $where['status'] = 1;
        //     $where['type_id'] = array('in','7,9');
        // }elseif ($manage_author['manage_type'] == 3) {
        //     if ($manage_author['username'] == 70000) {
                
        //     }else{
        //         $where['status'] = 1;
        //         $where['type_id'] = array('in','8,9');
        //     }
        // }else{

        // }
        if (isset($data['name'])) {
            if (!empty($data['name'])) {
                $where['name'] = array('like','%'.$data['name'].'%');
            }
        }
        if (isset($data['type_id'])) {
            if (!empty($data['type_id'])) {
                $where['type_id'] = $data['type_id'];
            }
        }
        if (isset($data['ids'])) {
            if (!empty($data['ids'])) {
                $where['id'] = array('in',implode(',', $data['ids']));
            }
        }

        if (isset($data['flag'])) {
            $productList = db('crm_product')->field('*,FROM_UNIXTIME(date,"%Y-%m-%d %H:%i:%s") as date')->where($where)->select();
        }else{
            $productList = db('crm_product')->field('*,FROM_UNIXTIME(date,"%Y-%m-%d %H:%i:%s") as date')->where($where)->order('date DESC')->limit(($data['page']-1)*$data['rows'],$data['rows'])->select();
        }
        $count = db('crm_product')->where($where)->count();

        $datas = array(
            'productList'=>$productList,
            'count'=>$count
        );

        apiReturn(200,'获取产品列表接口成功',$datas);
    }

    // 获取产品信息
    public function productInfo()
    {
        $data = input();//token id
        $productInfo = db('crm_product')->where('id',$data['id'])->find();
        apiReturn(200,'获取产品信息成功',$productInfo);
    }

    // 修改产品状态
    public function productStatus()
    {
        $data = input();//token id
        $status = db('crm_product')->where('id',$data['id'])->value('status');
        if ($status == 1) {
            $datas['status'] = 0;
        }else{
            $datas['status'] = 1;
        }
        db('crm_product')->where('id',$data['id'])->update($datas);
        apiReturn(200,'修改成功');
    }

    // 删除产品接口
    public function productDelete()
    {
        $data = input();//token id
        if (empty($data['id'])) {
            apiReturn(201,'产品id为空');
        }
        db('crm_product')->where('id',$data['id'])->delete();
        apiReturn(200,'删除成功');
    }

}
